package utility;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventType;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Jeppe
 */
public class StatistikModul {

    private final static JFXPanel jfxPanel = new JFXPanel();

    private static boolean initialized;

    private static void initFX(final ObservableList<PieChart.Data> pieDatas) {
        initialized = false;
        if(Platform.isFxApplicationThread()){
            System.out.println("Platform is FX");
        } else {
            System.out.println("Platform is not FX - you are in trouble");
        }
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Scene scene = createScene(pieDatas);
                jfxPanel.setScene(scene);
                System.out.println("jfx scene changed");
                initialized = true;
            }
        });
        
    }

    private static Scene createScene(final ObservableList<PieChart.Data> pieDatas) {
        ObservableList<PieChart.Data> pieChartData = pieDatas;
        final PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Statistik over besøgende");
        System.out.println(pieChartData.size());
        Group g = new Group();
        g.getChildren().add(chart);
        
        Scene scene = new Scene(g);
        System.out.println("Scene returned");
        return scene;
    }

    public static JFXPanel getJfxPanel(ObservableList<PieChart.Data> pieDatas) {
        System.out.println("trying to get updated jfx");
        initFX(pieDatas);
        System.out.println("init called");
        while (!initialized) {
            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {
                //Do nothing
            }
        }
        System.out.println("init complete - returning jfx");
        return jfxPanel;
    }

}
